﻿#include "chatsocket.h"
#include "fixedvalue.h"
#include "apply_protocl.h"
ChatSocket::ChatSocket(QObject *parent) : QObject(parent),client_socket(NULL)
{
    if (NULL == client_socket)
    {
        client_socket = new QTcpSocket();
    }
    client_socket->connectToHost(QHostAddress(DEFAULT_IP),DEFAULT_PORT);
    connect(client_socket,SIGNAL(readyRead()),this,SLOT(slotRecv()));
    qDebug()<<"connect "<<QHostAddress(DEFAULT_IP)<<DEFAULT_PORT;
}
void ChatSocket::slotRecv()
{
    /**解析包类型**/
    QByteArray allData = client_socket->readAll();//取出消息内容
    qDebug()<<allData;
    QString data = allData;
    //收到新数据
    QJsonParseError json_error;
    QJsonDocument jsonDoc(QJsonDocument::fromJson(data.toUtf8().data(), &json_error));
    if(json_error.error != QJsonParseError::NoError)
    {
        qDebug() << "json error!"<<json_error.errorString();
        return;
    }
    QJsonObject rootObj = jsonDoc.object();
    //因为是预先定义好的JSON数据格式，所以这里可以这样读取
    int type = -1;
    type = rootObj[NODE_TYPE].toInt();
    qDebug()<<type;
    switch (type) {
    case TYPE_LOGON_SUCCESS:    /*登录成功消息*/
        qDebug()<<rootObj[NODE_ID].toInt()<<rootObj[NODE_USERNAME].toString()<<rootObj[NODE_ICON].toString();
        emit logonSuccess(rootObj[NODE_ID].toInt(),rootObj[NODE_USERNAME].toString(),rootObj[NODE_ICON].toString());
        break;
    case TYPE_LOGON_ERROR:  /*登录失败消息*/
        emit logonFailed();
        break;
    case MESSAGE_FRIEND_LIST_RESPONSE:  /*好友列表响应*/
        emit friendListSucc(rootObj);
        break;
    case MESSAGE_CHAT_DATE: /*聊天消息*/
        emit chatDataPrivate(rootObj);
        break;
    case MESSAGE_REGISTER_RPT:  /*注册响应*/
        qDebug()<<"register response";
        emit registerRpt(rootObj);
        break;
    default:
        break;
    }

}

void ChatSocket::slotConnectToServer(QString ip, unsigned int port)
{
    //收到setinfo对象的success，与服务端建立连接
    qDebug()<<ip<<" "<<port;
    if (NULL == client_socket)
    {
        client_socket = new QTcpSocket();
    }
    client_socket->abort();
    //主动和服务器建立连接
    client_socket->connectToHost(QHostAddress(ip),port);
}

void ChatSocket::sendLogin(unsigned int id, QString user, QString password)
{
    //1. 创建各个节点元素
  QJsonObject json;
  json={{NODE_TYPE, TYPE_LOGIN},{NODE_ID, int(id)}, {NODE_USERNAME, user},  {NODE_PASSWORD, password}};
  QJsonDocument doc(json);   //直接使用构造函数
  QByteArray ba = doc.toJson(QJsonDocument::Indented);

  client_socket->write(ba);
}

void ChatSocket::sendMessage(QString message, int send_id, int recv_id)
{
    QJsonObject json;
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz ddd");
    json={{NODE_TYPE, MESSAGE_CHAT_DATE},{STR_SEND_ID, send_id},{STR_RECV_ID,recv_id},{STR_TIME,current_date},\
          {STR_MESSAGE_DATA,message}};
    QJsonDocument doc(json);   //直接使用构造函数
    QByteArray ba = doc.toJson(QJsonDocument::Indented);
    client_socket->write(ba);
}

void ChatSocket::getVerifyCode(QString mail)
{
    //获取验证码
    QJsonObject json;
    json={{NODE_TYPE, MESSAGE_VERIFY_REQ},{STR_MAIL,mail}};
    QJsonDocument doc(json);   //直接使用构造函数
    QByteArray ba = doc.toJson(QJsonDocument::Indented);
    client_socket->write(ba);
}

void ChatSocket::getFriendList(unsigned int id)
{
    QJsonObject json;

    json={{NODE_TYPE, MESSAGE_FRIEND_LIST},{STR_ID, int(id)}};
    QJsonDocument doc(json);   //直接使用构造函数
    QByteArray ba = doc.toJson(QJsonDocument::Indented);

    client_socket->write(ba);
}

void ChatSocket::registerRequest(QString icon, QString user_name, QString pwd, QString real_name, QString address, QString email, QString verify)
{
    //注册请求
    QJsonObject json;
    json={{NODE_TYPE, MESSAGE_REGISTER_REQ},{STR_ICON,icon},{STR_USERNAME,user_name},{STR_PASSWORD,pwd},\
         {STR_REAL_NAME,real_name},{STR_ADDRESS,address},{STR_MAIL,email},{STR_VERIFY_CODE,verify.toInt()}};
    QJsonDocument doc(json);   //直接使用构造函数
    QByteArray ba = doc.toJson(QJsonDocument::Indented);
    client_socket->write(ba);
}
